package com.hansen.library.pickerimage.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.hansen.library.R;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.pickerimage.constant.Extras;
import com.hansen.library.pickerimage.utils.BitmapUtil;
import com.hansen.library.pickerimage.utils.media.BitmapDecoder;
import com.hansen.library.pickerimage.widget.image.CropImageView;

public class CropImageActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    public static final int PICKER_IMAGE_EDIT = 0x1040;

    private boolean returnData;

    private String filePath;

    private NavigationBarLayout nav_crop_image;

    private CropImageView cropImageView;

    public static void startForData(Activity activity, String srcFile, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent(activity, CropImageActivity.class);
        intent.putExtra(Extras.EXTRA_SRC_FILE, srcFile);
        intent.putExtra(Extras.EXTRA_OUTPUTX, outputX);
        intent.putExtra(Extras.EXTRA_OUTPUTY, outputY);
        intent.putExtra(Extras.EXTRA_RETURN_DATA, true);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void startForFile(Activity activity, String srcFile, int outputX, int outputY, String path,
                                    int requestCode) {
        Intent intent = new Intent(activity, CropImageActivity.class);
        intent.putExtra(Extras.EXTRA_SRC_FILE, srcFile);
        intent.putExtra(Extras.EXTRA_OUTPUTX, outputX);
        intent.putExtra(Extras.EXTRA_OUTPUTY, outputY);
        intent.putExtra(Extras.EXTRA_FILE_PATH, path);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        nav_crop_image = findViewById(R.id.nav_crop_image);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        processIntent();

        initCropImageView();
    }

    @Override
    protected void initListener() {
        nav_crop_image.setOnNavgationBarClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {
        if (returnData) {
            byte[] data = cropImageView.getCroppedImage();
            if (data != null) {
                Intent intent = new Intent();
                intent.putExtra(Extras.EXTRA_DATA, data);
                setResult(RESULT_OK, intent);
            }
            finish();
        } else {
            if (cropImageView.saveCroppedImage(filePath)) {
                setResult(RESULT_OK);
            }
            finish();
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_crop_image;
    }

    private void processIntent() {
        Intent intent = getIntent();
        returnData = intent.getBooleanExtra(Extras.EXTRA_RETURN_DATA, false);
        filePath = intent.getStringExtra(Extras.EXTRA_FILE_PATH);
    }

    private void initCropImageView() {
        cropImageView = findViewById(R.id.cropable_image_view);
        Intent intent = getIntent();
        final String srcFile = intent.getStringExtra(Extras.EXTRA_SRC_FILE);
        int outputX = intent.getIntExtra(Extras.EXTRA_OUTPUTX, 0);
        int outputY = intent.getIntExtra(Extras.EXTRA_OUTPUTY, 0);
        cropImageView.setOutput(outputX, outputY);

        // 抛到下一个UI循环，等到我们的activity真正到了前台
        // 否则可能会获取不到openGL的最大texture size，导致解出的bitmap过大，显示不了
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                Bitmap src = BitmapDecoder.decodeSampledForDisplay(srcFile);
                src = BitmapUtil.rotateBitmapInNeeded(srcFile, src);
                cropImageView.setImageBitmap(src);
            }
        });
    }

    @Override
    protected void onDestroy() {
        cropImageView.clear();
        super.onDestroy();
    }
}
